{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "173939c0",
   "metadata": {},
   "source": [
    "### P058 数据聚类 - 使用Numpy自己实现聚类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "15552c66",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from numpy.linalg import norm\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5bcc7161",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "14f67470",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"p058.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1f9c5295",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.594675</td>\n",
       "      <td>-3.266358</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.865450</td>\n",
       "      <td>-3.762144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.299272</td>\n",
       "      <td>-3.166188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.781896</td>\n",
       "      <td>1.714512</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.733158</td>\n",
       "      <td>2.714493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2.479724</td>\n",
       "      <td>-2.412872</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.548970</td>\n",
       "      <td>-0.278338</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5.260191</td>\n",
       "      <td>-3.784969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1.594294</td>\n",
       "      <td>2.331512</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.376120</td>\n",
       "      <td>2.920965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>-0.513263</td>\n",
       "      <td>3.674438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1.428438</td>\n",
       "      <td>2.653099</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>3.082621</td>\n",
       "      <td>-2.231656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>-0.595741</td>\n",
       "      <td>3.431074</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1.021407</td>\n",
       "      <td>2.584693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.935124</td>\n",
       "      <td>4.341219</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1.402682</td>\n",
       "      <td>-4.070644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>1.161893</td>\n",
       "      <td>2.881046</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.634636</td>\n",
       "      <td>1.489924</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0.981140</td>\n",
       "      <td>4.805962</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0.977953</td>\n",
       "      <td>1.768433</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>4.088926</td>\n",
       "      <td>-2.906827</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>3.513604</td>\n",
       "      <td>-2.924726</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2.164466</td>\n",
       "      <td>-2.282778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>0.218400</td>\n",
       "      <td>3.155238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>4.771012</td>\n",
       "      <td>-3.128966</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>2.921052</td>\n",
       "      <td>-2.353398</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>3.555052</td>\n",
       "      <td>-3.663400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>1.972930</td>\n",
       "      <td>-2.683122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>0.809331</td>\n",
       "      <td>-3.996001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>-0.982497</td>\n",
       "      <td>3.084910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>-1.541495</td>\n",
       "      <td>-0.039335</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>2.438640</td>\n",
       "      <td>2.964188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>-0.600669</td>\n",
       "      <td>1.345953</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>3.666297</td>\n",
       "      <td>-4.135215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>-0.986650</td>\n",
       "      <td>2.496972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>2.822808</td>\n",
       "      <td>-2.022278</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>1.274411</td>\n",
       "      <td>-4.201629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>2.448127</td>\n",
       "      <td>-2.491829</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>1.264161</td>\n",
       "      <td>-2.179001</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          x1        x2\n",
       "0   2.594675 -3.266358\n",
       "1   1.865450 -3.762144\n",
       "2   1.299272 -3.166188\n",
       "3   0.781896  1.714512\n",
       "4  -0.733158  2.714493\n",
       "5   2.479724 -2.412872\n",
       "6  -0.548970 -0.278338\n",
       "7   5.260191 -3.784969\n",
       "8   1.594294  2.331512\n",
       "9   1.376120  2.920965\n",
       "10 -0.513263  3.674438\n",
       "11  1.428438  2.653099\n",
       "12  3.082621 -2.231656\n",
       "13 -0.595741  3.431074\n",
       "14  1.021407  2.584693\n",
       "15  0.935124  4.341219\n",
       "16  1.402682 -4.070644\n",
       "17  1.161893  2.881046\n",
       "18  0.634636  1.489924\n",
       "19  0.981140  4.805962\n",
       "20  0.977953  1.768433\n",
       "21  4.088926 -2.906827\n",
       "22  3.513604 -2.924726\n",
       "23  2.164466 -2.282778\n",
       "24  0.218400  3.155238\n",
       "25  4.771012 -3.128966\n",
       "26  2.921052 -2.353398\n",
       "27  3.555052 -3.663400\n",
       "28  1.972930 -2.683122\n",
       "29  0.809331 -3.996001\n",
       "30 -0.982497  3.084910\n",
       "31 -1.541495 -0.039335\n",
       "32  2.438640  2.964188\n",
       "33 -0.600669  1.345953\n",
       "34  3.666297 -4.135215\n",
       "35 -0.986650  2.496972\n",
       "36  2.822808 -2.022278\n",
       "37  1.274411 -4.201629\n",
       "38  2.448127 -2.491829\n",
       "39  1.264161 -2.179001"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "837ee151",
   "metadata": {},
   "outputs": [],
   "source": [
    "x1_min = df.x1.min()\n",
    "x1_max = df.x1.max()\n",
    "\n",
    "x2_min = df.x2.min()\n",
    "x2_max = df.x2.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0c643d8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-1.541495291463751, 5.260190687298884, -4.201628603933378, 4.805962184758963)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1_min, x1_max, x2_min, x2_max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ac733ba0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.8716163474758023"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.uniform(x1_min, x1_max)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3ac2453c",
   "metadata": {},
   "outputs": [],
   "source": [
    "centroid_1 = np.array([random.uniform(x1_min, x1_max), random.uniform(x2_min, x2_max)])\n",
    "centroid_2 = np.array([random.uniform(x1_min, x1_max), random.uniform(x2_min, x2_max)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b29d47bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 2.29061195, -4.18963305]), array([3.20304933, 1.38524521]))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "centroid_1, centroid_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "91f71027",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.59467534, -3.2663579 ],\n",
       "       [ 1.86544975, -3.76214406],\n",
       "       [ 1.29927175, -3.16618783],\n",
       "       [ 0.78189619,  1.71451194],\n",
       "       [-0.73315846,  2.71449347],\n",
       "       [ 2.47972356, -2.41287237],\n",
       "       [-0.5489699 , -0.27833828],\n",
       "       [ 5.26019069, -3.78496948],\n",
       "       [ 1.59429425,  2.33151219],\n",
       "       [ 1.37612008,  2.9209649 ],\n",
       "       [-0.51326277,  3.67443819],\n",
       "       [ 1.42843791,  2.65309909],\n",
       "       [ 3.08262056, -2.23165636],\n",
       "       [-0.59574102,  3.43107396],\n",
       "       [ 1.02140747,  2.58469281],\n",
       "       [ 0.93512419,  4.34121897],\n",
       "       [ 1.40268169, -4.07064361],\n",
       "       [ 1.16189327,  2.88104551],\n",
       "       [ 0.6346358 ,  1.48992406],\n",
       "       [ 0.98114022,  4.80596218],\n",
       "       [ 0.97795318,  1.76843278],\n",
       "       [ 4.08892614, -2.90682729],\n",
       "       [ 3.51360407, -2.92472641],\n",
       "       [ 2.16446616, -2.28277776],\n",
       "       [ 0.21840007,  3.15523793],\n",
       "       [ 4.771012  , -3.1289664 ],\n",
       "       [ 2.92105241, -2.35339834],\n",
       "       [ 3.55505191, -3.66340036],\n",
       "       [ 1.97293022, -2.68312242],\n",
       "       [ 0.8093305 , -3.99600058],\n",
       "       [-0.98249667,  3.08490955],\n",
       "       [-1.54149529, -0.03933492],\n",
       "       [ 2.43863956,  2.96418837],\n",
       "       [-0.6006694 ,  1.34595331],\n",
       "       [ 3.66629721, -4.13521532],\n",
       "       [-0.98665024,  2.49697185],\n",
       "       [ 2.8228083 , -2.02227828],\n",
       "       [ 1.27441095, -4.2016286 ],\n",
       "       [ 2.44812697, -2.49182884],\n",
       "       [ 1.2641607 , -2.17900119]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = df.values\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "89f740d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10):\n",
    "    clusters = []\n",
    "    for point in data:\n",
    "        centroid_1_dist = norm(centroid_1 - point)\n",
    "        centroid_2_dist = norm(centroid_2 - point)\n",
    "        cluster = 1\n",
    "        if centroid_1_dist > centroid_2_dist:\n",
    "            cluster = 2\n",
    "        clusters.append(cluster)\n",
    "    df[\"cluster\"] = clusters\n",
    "    \n",
    "    centroid_1 = [round(df[df.cluster == 1].x1.mean(),3), round(df[df.cluster == 1].x2.mean(),3)]\n",
    "    centroid_2 = [round(df[df.cluster == 2].x1.mean(),3), round(df[df.cluster == 2].x2.mean(),3)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "85ca4312",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([2.663, -3.083], [0.352, 2.502])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "centroid_1, centroid_2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13cb070d",
   "metadata": {},
   "source": [
    "### P059 数据聚类 - KMeans实现聚类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e68b528d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.cluster import KMeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3beac2e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c5246a22",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"p059.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "be473744",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.776333</td>\n",
       "      <td>-4.166641</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.335879</td>\n",
       "      <td>-1.083934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6.507272</td>\n",
       "      <td>-0.158773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.956622</td>\n",
       "      <td>0.235036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.558383</td>\n",
       "      <td>-3.969630</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         x1        x2\n",
       "0 -2.776333 -4.166641\n",
       "1 -1.335879 -1.083934\n",
       "2  6.507272 -0.158773\n",
       "3 -0.956622  0.235036\n",
       "4 -1.558383 -3.969630"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c87135aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "kmeans = KMeans(\n",
    "    n_clusters=3,\n",
    "    max_iter=100, \n",
    "    random_state=42\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "03872b28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KMeans(max_iter=100, n_clusters=3, random_state=42)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmeans.fit(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b1be3636",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.55537629, -0.32971364],\n",
       "       [ 4.86661316,  0.42352176],\n",
       "       [-2.15656147, -4.30478556]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmeans.cluster_centers_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cb57b7d",
   "metadata": {},
   "source": [
    "### P060 数据聚类 - 使用KMeans模型实现预估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b5378e14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.776333</td>\n",
       "      <td>-4.166641</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.335879</td>\n",
       "      <td>-1.083934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6.507272</td>\n",
       "      <td>-0.158773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.956622</td>\n",
       "      <td>0.235036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.558383</td>\n",
       "      <td>-3.969630</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         x1        x2\n",
       "0 -2.776333 -4.166641\n",
       "1 -1.335879 -1.083934\n",
       "2  6.507272 -0.158773\n",
       "3 -0.956622  0.235036\n",
       "4 -1.558383 -3.969630"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "be479b17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KMeans(max_iter=100, n_clusters=3, random_state=42)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "17865231",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_kmeans = kmeans.predict(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "bf9292ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 0, 1, 0, 2, 0, 1, 2, 1, 1])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_kmeans[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e7f17f08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000,)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_kmeans.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "be10edea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000, 2)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3ee62978",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"y_kmeans\"] = y_kmeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f7a03f17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>y_kmeans</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.776333</td>\n",
       "      <td>-4.166641</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.335879</td>\n",
       "      <td>-1.083934</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6.507272</td>\n",
       "      <td>-0.158773</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.956622</td>\n",
       "      <td>0.235036</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.558383</td>\n",
       "      <td>-3.969630</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>-2.082539</td>\n",
       "      <td>-4.016371</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>4.917095</td>\n",
       "      <td>3.206841</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>-3.683461</td>\n",
       "      <td>-5.141359</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>-3.460697</td>\n",
       "      <td>-3.756673</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>-0.742855</td>\n",
       "      <td>-1.290850</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           x1        x2  y_kmeans\n",
       "0   -2.776333 -4.166641         2\n",
       "1   -1.335879 -1.083934         0\n",
       "2    6.507272 -0.158773         1\n",
       "3   -0.956622  0.235036         0\n",
       "4   -1.558383 -3.969630         2\n",
       "..        ...       ...       ...\n",
       "995 -2.082539 -4.016371         2\n",
       "996  4.917095  3.206841         1\n",
       "997 -3.683461 -5.141359         2\n",
       "998 -3.460697 -3.756673         2\n",
       "999 -0.742855 -1.290850         0\n",
       "\n",
       "[1000 rows x 3 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e91c099",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "2c530fda",
   "metadata": {},
   "source": [
    "### P061 061 数据聚类 - 评价指标之WCSS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "69f9735c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.cluster import KMeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0405c3c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "18674e1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"./p061.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "dd2c9045",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-6.645730</td>\n",
       "      <td>5.470461</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-4.390559</td>\n",
       "      <td>-5.055149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-4.666211</td>\n",
       "      <td>-5.390080</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         x1        x2\n",
       "0 -6.645730  5.470461\n",
       "1 -4.390559 -5.055149\n",
       "2 -4.666211 -5.390080"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "42fd9bd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[23414.040458695985,\n",
       " 7734.886635126092,\n",
       " 4282.137026938603,\n",
       " 3851.546834210853,\n",
       " 3442.659536944271,\n",
       " 3049.7421351793673,\n",
       " 2716.8660768161135,\n",
       " 2472.8024581628047]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wcss = []\n",
    "for i in range(2, 10):\n",
    "    kmeans = KMeans(n_clusters=i, random_state=42)\n",
    "    kmeans.fit(df)\n",
    "    wcss.append(kmeans.inertia_)\n",
    "wcss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "881a37d8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "96b48fa6",
   "metadata": {},
   "source": [
    "### P062 数据聚类 - 肘部法则选择聚类数目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "742ba391",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "dcbc82ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x19ff5cab550>]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD4CAYAAADsKpHdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cH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pm4CPht78ZqDVzP4p2pIm5u7HwuNJ4Adk+rL56ihwNGeG+CSZgMhnHwJedPcTURcyhX8HHHb3NncfAr4PfCDimibk7o+5+7XufgtwCpi16wdQZIEQLtQ+Bux197+Lup7JmFmTmS0OzyvI/I+7L9KiJuHuD7r7cndfSaZVsNXd8/JfWmZWFRYVEFovv09mOp6X3P1t4IiZNYehDUDeLYQY45PkebsoeBNYb2aV4e+HDWSuLeYlM0uGx0uBP2KW/xvHZ/PDCsBNwB8DL4fePMBfu/tPoitpQkuBTWGlRgx4wt3zeilnAVkC/CDz55848B13fyrakqb0Z8C3QyvmNeBPI65nQqG/fTvwmahrmYq7P29mTwIvkmm/vER+/xqL75lZAzAE3O/unbP54UW17FRERCZWVC0jERGZmAJBREQABYKIiAQKBBERARQIIiISKBBERARQIIiISPD/ASBO1q03HzGYAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(\n",
    "    list(range(2, 10)),\n",
    "    wcss\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb23bfa9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f96d903",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02d291de",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "005570b8",
   "metadata": {},
   "source": [
    "### P063 数据聚类 - 层次聚类AgglomerativeClustering"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ceaf1eb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.cluster import AgglomerativeClustering"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "21a60229",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"./p063.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d4994d17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.486532</td>\n",
       "      <td>7.025770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-3.522549</td>\n",
       "      <td>8.578303</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.982040</td>\n",
       "      <td>7.998514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.135276</td>\n",
       "      <td>6.255888</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.762504</td>\n",
       "      <td>4.210918</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         x1        x2\n",
       "0 -2.486532  7.025770\n",
       "1 -3.522549  8.578303\n",
       "2 -2.982040  7.998514\n",
       "3 -2.135276  6.255888\n",
       "4  2.762504  4.210918"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "18d2d0bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "cluster = AgglomerativeClustering(n_clusters=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "baad4794",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0,\n",
       "       0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1,\n",
       "       1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,\n",
       "       0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0,\n",
       "       0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1,\n",
       "       1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1,\n",
       "       0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,\n",
       "       0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0,\n",
       "       0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1,\n",
       "       0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1,\n",
       "       1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1,\n",
       "       1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0,\n",
       "       1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1,\n",
       "       1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,\n",
       "       1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1,\n",
       "       1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1,\n",
       "       0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0,\n",
       "       0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0,\n",
       "       0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0,\n",
       "       0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1,\n",
       "       0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1], dtype=int64)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cluster.fit_predict(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "0aa91d70",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"cluster\"] = cluster.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c7dfe5cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>cluster</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.486532</td>\n",
       "      <td>7.025770</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-3.522549</td>\n",
       "      <td>8.578303</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.982040</td>\n",
       "      <td>7.998514</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.135276</td>\n",
       "      <td>6.255888</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.762504</td>\n",
       "      <td>4.210918</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>495</th>\n",
       "      <td>-1.684640</td>\n",
       "      <td>6.384198</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>496</th>\n",
       "      <td>3.909988</td>\n",
       "      <td>1.434175</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>497</th>\n",
       "      <td>4.350496</td>\n",
       "      <td>-0.082984</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>498</th>\n",
       "      <td>-1.295743</td>\n",
       "      <td>6.086787</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499</th>\n",
       "      <td>3.087084</td>\n",
       "      <td>1.604627</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>500 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           x1        x2  cluster\n",
       "0   -2.486532  7.025770        0\n",
       "1   -3.522549  8.578303        0\n",
       "2   -2.982040  7.998514        0\n",
       "3   -2.135276  6.255888        0\n",
       "4    2.762504  4.210918        1\n",
       "..        ...       ...      ...\n",
       "495 -1.684640  6.384198        0\n",
       "496  3.909988  1.434175        1\n",
       "497  4.350496 -0.082984        1\n",
       "498 -1.295743  6.086787        0\n",
       "499  3.087084  1.604627        1\n",
       "\n",
       "[500 rows x 3 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51798821",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b1fbda0f",
   "metadata": {},
   "source": [
    "### P064 数据聚类 - 层次聚类的重要属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f68c019e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.cluster import AgglomerativeClustering"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "4432753e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"./p063.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "deb03363",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.486532</td>\n",
       "      <td>7.025770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-3.522549</td>\n",
       "      <td>8.578303</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.982040</td>\n",
       "      <td>7.998514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.135276</td>\n",
       "      <td>6.255888</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.762504</td>\n",
       "      <td>4.210918</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         x1        x2\n",
       "0 -2.486532  7.025770\n",
       "1 -3.522549  8.578303\n",
       "2 -2.982040  7.998514\n",
       "3 -2.135276  6.255888\n",
       "4  2.762504  4.210918"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "39b834df",
   "metadata": {},
   "outputs": [],
   "source": [
    "cluster = AgglomerativeClustering(\n",
    "    n_clusters=2,\n",
    "    linkage=\"complete\",\n",
    "    affinity=\"manhattan\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "6937a6bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0,\n",
       "       0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1,\n",
       "       1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,\n",
       "       0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0,\n",
       "       0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1,\n",
       "       1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1,\n",
       "       0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,\n",
       "       0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0,\n",
       "       0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1,\n",
       "       0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1,\n",
       "       1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1,\n",
       "       1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0,\n",
       "       1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1,\n",
       "       1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,\n",
       "       1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1,\n",
       "       1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1,\n",
       "       0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0,\n",
       "       0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0,\n",
       "       0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0,\n",
       "       0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1,\n",
       "       0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1], dtype=int64)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cluster.fit_predict(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "fafcfd9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(df, columns=['x1', 'x2'])\n",
    "df[\"cluster\"] = cluster.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "4410691e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>cluster</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.486532</td>\n",
       "      <td>7.025770</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-3.522549</td>\n",
       "      <td>8.578303</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.982040</td>\n",
       "      <td>7.998514</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.135276</td>\n",
       "      <td>6.255888</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.762504</td>\n",
       "      <td>4.210918</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         x1        x2  cluster\n",
       "0 -2.486532  7.025770        0\n",
       "1 -3.522549  8.578303        0\n",
       "2 -2.982040  7.998514        0\n",
       "3 -2.135276  6.255888        0\n",
       "4  2.762504  4.210918        1"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da81d172",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c1c2ca05",
   "metadata": {},
   "source": [
    "### P065 数据聚类 - DBSCAN聚类算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6ff3e04f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.cluster import DBSCAN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4542f860",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"./p065.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b8aebc96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-6.730717</td>\n",
       "      <td>5.548132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-4.613187</td>\n",
       "      <td>-5.144937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-4.833709</td>\n",
       "      <td>-5.412881</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.160670</td>\n",
       "      <td>0.558723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.400393</td>\n",
       "      <td>-0.225236</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         x1        x2\n",
       "0 -6.730717  5.548132\n",
       "1 -4.613187 -5.144937\n",
       "2 -4.833709 -5.412881\n",
       "3  3.160670  0.558723\n",
       "4  3.400393 -0.225236"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2da091bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "cluster = DBSCAN(eps=0.6, min_samples=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "309f63d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DBSCAN(eps=0.6, min_samples=7)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cluster.fit(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5c5d9423",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"cluster\"] = cluster.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2fa1f236",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>cluster</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-6.730717</td>\n",
       "      <td>5.548132</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-4.613187</td>\n",
       "      <td>-5.144937</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-4.833709</td>\n",
       "      <td>-5.412881</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.160670</td>\n",
       "      <td>0.558723</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.400393</td>\n",
       "      <td>-0.225236</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         x1        x2  cluster\n",
       "0 -6.730717  5.548132        0\n",
       "1 -4.613187 -5.144937        1\n",
       "2 -4.833709 -5.412881        1\n",
       "3  3.160670  0.558723        2\n",
       "4  3.400393 -0.225236        2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "721f44a9",
   "metadata": {},
   "source": [
    "### P066 数据聚类 - DBSCAN聚类算法的异常点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9dff7359",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"./p065.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7b9024a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-6.730717</td>\n",
       "      <td>5.548132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-4.613187</td>\n",
       "      <td>-5.144937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-4.833709</td>\n",
       "      <td>-5.412881</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.160670</td>\n",
       "      <td>0.558723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.400393</td>\n",
       "      <td>-0.225236</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         x1        x2\n",
       "0 -6.730717  5.548132\n",
       "1 -4.613187 -5.144937\n",
       "2 -4.833709 -5.412881\n",
       "3  3.160670  0.558723\n",
       "4  3.400393 -0.225236"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7152a635",
   "metadata": {},
   "outputs": [],
   "source": [
    "cluster = DBSCAN(eps=0.6, min_samples=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d477b117",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DBSCAN(eps=0.6, min_samples=7)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cluster.fit(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4633ec11",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"cluster\"] = cluster.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1ecb5ff3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>cluster</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-6.730717</td>\n",
       "      <td>5.548132</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-4.613187</td>\n",
       "      <td>-5.144937</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-4.833709</td>\n",
       "      <td>-5.412881</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.160670</td>\n",
       "      <td>0.558723</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.400393</td>\n",
       "      <td>-0.225236</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         x1        x2  cluster\n",
       "0 -6.730717  5.548132        0\n",
       "1 -4.613187 -5.144937        1\n",
       "2 -4.833709 -5.412881        1\n",
       "3  3.160670  0.558723        2\n",
       "4  3.400393 -0.225236        2"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b3d50369",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       " 0    449\n",
       " 1    232\n",
       " 2    231\n",
       "-1     88\n",
       "Name: cluster, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"cluster\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63843485",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
